package zju.ccnt.rest.service.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by zha on 14/11/21.
 */
public class EnumValueValidator implements ConstraintValidator<ValidEnumValue, String> {

    Class targetEnumClass;

    @Override
    public void initialize(ValidEnumValue validEnumValue) {
        this.targetEnumClass = validEnumValue.enumClassName();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        Object[] validEnumConstants = targetEnumClass.getEnumConstants();
        for (Object validEnumConstant : validEnumConstants){
            if (s.equals(validEnumConstant.toString())) return true;
        }
        return false;
    }
}
